package com.cskaoyan.generator;

import com.cskaoyan.service.UserService;
import com.cskaoyan.service.UserServiceImpl;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author stone
 * @date 2022/03/22 20:19
 */
public class CglibGenerator {

    public static void main(String[] args) {
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\WorkSpace\\j37_workspace\\codes\\spring\\day03-aop\\demo7-dynamic-proxy");
        UserService userService = new UserServiceImpl();
        UserService proxy = (UserService) Enhancer.create(UserServiceImpl.class, new net.sf.cglib.proxy.InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                System.out.println("委托类执行方法之前");
                Object invoke = method.invoke(userService, objects);
                System.out.println("委托类执行方法之后");

                return invoke;
            }
        });
        proxy.sayHello("songge");
    }
}
